/**
 * Created by WYH on 16/12/30.
 */
$(document).ready(function () {
    JudgeBrowser.changeTopBarTextSizeInIE();
    if (JudgeLogin.isLogin()) {
        setupManagerName();
        bindClickEvent();
        JudgeLogin.bindSignOut(".signOut");
    }
});

function setupManagerName() {
    Api.fetchCompanyDetailInfo(function (result) {
        $(".managerBaseInfo").text("管理员：" + result.data.companyName);
    }, function (error) {
        alert("获取管理员名字失败");
    })
}

function bindClickEvent() {
    clickPasswordLock(".oldPassword");
    clickPasswordLock(".newPassword");
    clickPasswordLock(".confirmNewPassword");
    clickConfirmChangeButton();
}

function clickPasswordLock(module) {

    $(module + "Lock").click(function () {

        var moduleType = $(module).attr("type");
        $(module).attr("type", moduleType === "text" ? "password" : "text");
    });
}

function clickConfirmChangeButton() {
    $(".confirmChangeButton").click(function () {
        var oldPassword = $(".oldPassword").val();
        var newPassword = $(".newPassword").val();
        var confirmNewPassword = $(".confirmNewPassword").val();
        if (judgePasswordIsValid(oldPassword, newPassword, confirmNewPassword)) {
            //post  newPassword,  oldPassword
            changeCompanyPassword(oldPassword, newPassword);
        }
    });
}


function handleChangePasswordResult(result) {
    if (result.code == 200) {
        alert("更改成功");
        window.location.href = "../../html/company/companyInfo.html";
    } else if (result.code == 409) {
        alert('旧密码错误');
    }
}


function changeCompanyPassword(oldPassword, newPassword) {
    Api.changeCompanyPassword(oldPassword, newPassword, function (result) {
        console.log(result);
        handleChangePasswordResult(result);
    }, function (error) {
        console.log(error);
        alert("更改失败");
    });



}


function judgePasswordIsValid(oldPassword, newPassword, confirmNewPassword) {
    if (oldPassword == "" || newPassword == "" || confirmNewPassword == "") {
        alert("请填写完整信息");
        return false;
    } else if (newPassword != confirmNewPassword){
        alert("输入的两次密码不相同");
        return false
    } else {
        return true;
    }
}
